﻿using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
partial struct ShootAttackSystem : ISystem
{
    [BurstCompile]
    public void OnCreate(ref SystemState state)
    {
        state.RequireForUpdate<EntitiesReferences>();
    }

    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        EntitiesReferences entitiesReferences = SystemAPI.GetSingleton<EntitiesReferences>();
        foreach ((RefRW<LocalTransform> localTransform
            , RefRW<ShootAttack> shootAttack
            , RefRO<Target> target
            , RefRO<UnitMover> unitMover
            , RefRW<TargetPositionPathQueued> targetPositionPathQueued
            , EnabledRefRW<TargetPositionPathQueued> targetPositionPathQueuedEnabled
            , Entity entity)
            in SystemAPI.Query<RefRW<LocalTransform>
            , RefRW<ShootAttack>
            , RefRO<Target>
            , RefRO<UnitMover>
             , RefRW<TargetPositionPathQueued> 
            , EnabledRefRW< TargetPositionPathQueued > > ().WithDisabled<MoveOverride>().WithPresent<TargetPositionPathQueued>().WithEntityAccess())
        {
            if (target.ValueRO.tagertEntity == Entity.Null)
            {
                continue;
            }

            LocalTransform tagertLocalTransform = SystemAPI.GetComponent<LocalTransform>(target.ValueRO.tagertEntity);
            if (math.distance(tagertLocalTransform.Position, localTransform.ValueRO.Position) > shootAttack.ValueRO.attackDiatance)
            {
                //如果距离太远 移动向目标
                targetPositionPathQueued.ValueRW.targetPostion = tagertLocalTransform.Position;
                targetPositionPathQueuedEnabled.ValueRW = true;
                continue;
            }
            else
            {
                ///如果达到设计距离 那么停留射击
                targetPositionPathQueued.ValueRW.targetPostion = localTransform.ValueRO.Position;
                targetPositionPathQueuedEnabled.ValueRW = true;
            }




            float3 aimDirection = tagertLocalTransform.Position - localTransform.ValueRO.Position;
            aimDirection = math.normalize(aimDirection);

            quaternion targetRotation = quaternion.LookRotation(aimDirection, math.up());

            localTransform.ValueRW.Rotation = 
                math.slerp(localTransform.ValueRO.Rotation,targetRotation,SystemAPI.Time.DeltaTime * unitMover.ValueRO.rotationSpeed);

        }

        foreach ((RefRW<LocalTransform> localTransform, RefRW<ShootAttack> shootAttack, RefRO<Target> target,Entity entity)
    in SystemAPI.Query<RefRW<LocalTransform>, RefRW<ShootAttack>, RefRO<Target>>().WithEntityAccess())
        {
            if (target.ValueRO.tagertEntity == Entity.Null)
            {
                continue;
            }

            LocalTransform tagertLocalTransform = SystemAPI.GetComponent<LocalTransform>(target.ValueRO.tagertEntity);

            if (math.distance(localTransform.ValueRO.Position, tagertLocalTransform.Position) > shootAttack.ValueRO.attackDiatance)
            {
                //目标太远不进行攻击
                continue;
            }

            if (SystemAPI.HasComponent<MoveOverride>(entity) && SystemAPI.IsComponentEnabled<MoveOverride>(entity))
            {
                //如果移动覆盖组件是激活状态说明他正在手玩家手动操作 移动中那么也不进行攻击
                continue;
            }

            shootAttack.ValueRW.timer -= SystemAPI.Time.DeltaTime;
            if (shootAttack.ValueRW.timer > 0)
            {
                continue;
            }
            shootAttack.ValueRW.timer = shootAttack.ValueRO.timerMax;

            //判断目标是否有目标覆盖组件
            if (SystemAPI.HasComponent<TargetOverride>(target.ValueRO.tagertEntity))
            {
                //僵尸被攻击 抄攻击者移动
                RefRW<TargetOverride> enemyTargetOverride = SystemAPI.GetComponentRW<TargetOverride>(target.ValueRO.tagertEntity);
                if (enemyTargetOverride.ValueRW.tagertEntity == Entity.Null)
                {
                    enemyTargetOverride.ValueRW.tagertEntity = entity;
                }
            }

            //生成子弹实体
            Entity bulletEntity = state.EntityManager.Instantiate(entitiesReferences.bulletPrefabEntity);
            //将位置设置为与该单位相同的位置
            float3 bulletSpawnWorldPostion = localTransform.ValueRO.TransformPoint(shootAttack.ValueRO.bulletSpawnLocalPostion);
            SystemAPI.SetComponent(bulletEntity, LocalTransform.FromPosition(bulletSpawnWorldPostion));

            //设置子弹实体组件Bullet的数据
            RefRW<Bullet> bulletBullet = SystemAPI.GetComponentRW<Bullet>(bulletEntity);
            bulletBullet.ValueRW.damageAmount = shootAttack.ValueRW.damageAmount;

            ///设置子弹实体组件Target的数据
            RefRW<Target> bullettarget = SystemAPI.GetComponentRW<Target>(bulletEntity);
            bullettarget.ValueRW.tagertEntity = target.ValueRO.tagertEntity;

            shootAttack.ValueRW.onShoot.isTriggered = true;
            shootAttack.ValueRW.onShoot.shootFromPostion = bulletSpawnWorldPostion;
            /////生成灯光 设置灯光位置
            //Entity shootLightEntity = state.EntityManager.Instantiate(entitiesReferences.shootlightPrefabEntity);
            //SystemAPI.SetComponent(shootLightEntity, LocalTransform.FromPosition(bulletSpawnWorldPostion));

        }
    }
}
